package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.MedicalEquipment;
import com.ruoyi.system.mapper.HospitalsMapper;
import com.ruoyi.system.domain.Hospitals;
import com.ruoyi.system.service.IHospitalsService;

/**
 * 医院基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-02
 */
@Service
public class HospitalsServiceImpl implements IHospitalsService 
{
    @Autowired
    private HospitalsMapper hospitalsMapper;

    /**
     * 查询医院基本信息
     * 
     * @param hospitalId 医院基本信息主键
     * @return 医院基本信息
     */
    @Override
    public Hospitals selectHospitalsByHospitalId(Long hospitalId)
    {
        return hospitalsMapper.selectHospitalsByHospitalId(hospitalId);
    }

    /**
     * 查询医院基本信息列表
     * 
     * @param hospitals 医院基本信息
     * @return 医院基本信息
     */
    @Override
    public List<Hospitals> selectHospitalsList(Hospitals hospitals)
    {
        return hospitalsMapper.selectHospitalsList(hospitals);
    }

    /**
     * 新增医院基本信息
     * 
     * @param hospitals 医院基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHospitals(Hospitals hospitals)
    {
        int rows = hospitalsMapper.insertHospitals(hospitals);
        insertMedicalEquipment(hospitals);
        return rows;
    }

    /**
     * 修改医院基本信息
     * 
     * @param hospitals 医院基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHospitals(Hospitals hospitals)
    {
        hospitalsMapper.deleteMedicalEquipmentByHospitalId(hospitals.getHospitalId());
        insertMedicalEquipment(hospitals);
        return hospitalsMapper.updateHospitals(hospitals);
    }

    /**
     * 批量删除医院基本信息
     * 
     * @param hospitalIds 需要删除的医院基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHospitalsByHospitalIds(Long[] hospitalIds)
    {
        hospitalsMapper.deleteMedicalEquipmentByHospitalIds(hospitalIds);
        return hospitalsMapper.deleteHospitalsByHospitalIds(hospitalIds);
    }

    /**
     * 删除医院基本信息信息
     * 
     * @param hospitalId 医院基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHospitalsByHospitalId(Long hospitalId)
    {
        hospitalsMapper.deleteMedicalEquipmentByHospitalId(hospitalId);
        return hospitalsMapper.deleteHospitalsByHospitalId(hospitalId);
    }

    /**
     * 新增医疗器材信息信息
     * 
     * @param hospitals 医院基本信息对象
     */
    public void insertMedicalEquipment(Hospitals hospitals)
    {
        List<MedicalEquipment> medicalEquipmentList = hospitals.getMedicalEquipmentList();
        Long hospitalId = hospitals.getHospitalId();
        if (StringUtils.isNotNull(medicalEquipmentList))
        {
            List<MedicalEquipment> list = new ArrayList<MedicalEquipment>();
            for (MedicalEquipment medicalEquipment : medicalEquipmentList)
            {
                medicalEquipment.setHospitalId(hospitalId);
                list.add(medicalEquipment);
            }
            if (list.size() > 0)
            {
                hospitalsMapper.batchMedicalEquipment(list);
            }
        }
    }
}
